#!/usr/bin/env python
# encoding: utf-8
import pika

credentials = pika.PlainCredentials("guest", "guest")
conn_params = pika.ConnectionParameters("localhost",credentials=credentials)
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()

channel.exchange_declare(exchange="exchange19", 
                         exchange_type="direct",
                         passive=False,
                         durable=False,
                         auto_delete=False)
#测试死信队列
#channel.queue_declare(queue="ttl_long-queue",arguments={"x-expires":60000,"x-max-length":1000,"x-dead-letter-exchange":"dead_exchange","x-dead-letter-routing-key":"hello"}) #过期时间单位ms

#测试消息超时时间
channel.queue_declare(queue="ttl_long-queue",arguments={"x-message-ttl":60000,"x-expires":40000,"x-max-length":1000,"x-dead-letter-exchange":"dead_exchange","x-dead-letter-routing-key":"hello"}) #过期时间单位ms

channel.exchange_declare(exchange="dead_exchange",exchange_type="fanout")
channel.queue_declare(queue="dead_queue")
channel.queue_bind(queue="dead_queue",exchange="dead_exchange",routing_key="hello")

channel.queue_bind(queue="ttl_long-queue", 
                   exchange="exchange19",
                   routing_key="hola")

#channel.basic_publish(body="message",exchange="exchange19",routing_key="hola")
msg_props = pika.BasicProperties()
msg_props.content_type = "text/plain"
msg_props.expiration = "30000"
for n in range(15):
   channel.basic_publish(body="message",
                      exchange="exchange19",
                      properties=msg_props,
                      routing_key="hola")

msg_props.expiration = "80000"
for n in range(15):
   channel.basic_publish(body="message",
                      exchange="exchange19",
                      properties=msg_props,
                      routing_key="hola")

msg_props.expiration = "30000"
for n in range(15):
   channel.basic_publish(body="message",
                      exchange="exchange19",
                      properties=msg_props,
                      routing_key="hola")
channel.basic_publish(body="message",exchange="exchange19",routing_key="hola")
